package com.java.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.service.AccountService;
import com.java.service.AdminService;
import com.java.util.resp.BaseRespone;
import com.java.util.servlet.ServletUtil;

@Controller
public class IndexController {
	private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired private AdminService adminService;
	@Autowired private AccountService accountService;
	@Autowired HttpServletRequest request;

	@RequestMapping("/")
	public String def() {
		log.info("def");
		if(ServletUtil.adminIsLogin()) {
			return "admin/info";
		}
		if(ServletUtil.userIsLogin()) {
			if(ServletUtil.isStudent()) {
				return "student/info";
			}
			if(ServletUtil.isTeacher()) {
				return "teacher/info";
			}
			if(ServletUtil.isGroup()) {
				return "group/info";
			}
		}
		return "student/login";
	}

	@RequestMapping(value = "/admin/{fileName}", method = RequestMethod.GET)
	public String admin(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.adminIsLogin()) {
			return "admin/info";
		}
		if(ServletUtil.adminIsLogin() && (fileName.equals("student") ||fileName.equals("teacher") || fileName.equals("group"))) {
			return "admin/raceAccount";
		}
		return "admin/" + fileName;
	}

	@RequestMapping(value = "/admin/logon", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone adminLogon(String user, String password) {
		Object URI = request.getSession().getAttribute("URI");
		BaseRespone resp = adminService.login(user, password);
		if(null != URI) {
			resp.setData(URI.toString());
		}
		return resp;
	}

	@RequestMapping(value = "/admin/logout", method = RequestMethod.GET)
	public String adminlogout() {
		ServletUtil.getSession().invalidate();
		return "admin/login";
	}
	
	/**
	 * 学生操作
	 */
	@RequestMapping(value = "/student/{fileName}", method = RequestMethod.GET)
	public String student(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.userIsLogin() && ServletUtil.isStudent()) {
			return "student/info";
		}
		return "student/" + fileName;
	}
	@RequestMapping(value = "/student/raceInfoDetail/{infoId}", method = RequestMethod.GET)
	public String raceInfoDetail(@PathVariable String infoId, Model model) {
		model.addAttribute("infoId", infoId);
		return "student/raceInfoDetail";
	}

	@RequestMapping(value = "/student/logon", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone studentLogon(String number, String password) {
		return accountService.login(0, number, password);
	}

	@RequestMapping(value = "/student/logout", method = RequestMethod.GET)
	public String studentlogout() {
		ServletUtil.getSession().invalidate();
		return "redirect:/student/login";
	}

	@RequestMapping(value = "/student/register", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone studentregister(String number, String password, String name) {
		return accountService.register(0, number, password, name);
	}
	
	/**
	 * 老师操作
	 */
	@RequestMapping(value = "/teacher/{fileName}", method = RequestMethod.GET)
	public String teacher(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.userIsLogin() && ServletUtil.isTeacher()) {
			return "teacher/info";
		}
		return "teacher/" + fileName;
	}

	@RequestMapping(value = "/teacher/logon", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone teacherLogon(String number, String password) {
		return accountService.login(1, number, password);
	}

	@RequestMapping(value = "/teacher/logout", method = RequestMethod.GET)
	public String teacherlogout() {
		ServletUtil.getSession().invalidate();
		return "redirect:/teacher/login";
	}

	@RequestMapping(value = "/teacher/register", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone teacherregister(String number, String password, String name) {
		return accountService.register(1, number, password, name);
	}
	
	/**
	 * 团委操作
	 */
	@RequestMapping(value = "/group/{fileName}", method = RequestMethod.GET)
	public String group(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.userIsLogin() && ServletUtil.isGroup()) {
			return "group/info";
		}
		return "group/" + fileName;
	}

	@RequestMapping(value = "/group/logon", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone groupLogon(String number, String password) {
		return accountService.login(2, number, password);
	}

	@RequestMapping(value = "/group/logout", method = RequestMethod.GET)
	public String grouplogout() {
		ServletUtil.getSession().invalidate();
		return "redirect:/group/login";
	}

}
